import math
import os
import time
import sys

# 将工程目录加入包扫描
currPath = os.path.abspath(os.path.dirname(__file__))
srcPath = os.path.split(currPath)[0]
rootPath = os.path.split(srcPath)[0]
projectPath = os.path.split(rootPath)[0]
sys.path.append(projectPath)

from src.db.CcxDataCenterDb import CcxDataCenter
from src.db.FCDB import FCDB
from src.db.ProdDb import ProdDb
from src.db.CnabsDb import CnabsDb
from src.tasks.abs.SyncAbsDeals import SyncAbsDeals
from src.tasks.abs.SyncAbsTables import SyncAbsTables
from src.tasks.abs.SyncAbsBigData import SyncAbsBigData
from src.utils.CommonUtils import cleanScreen
from src.utils.LogUtils import Logger


# 主函数
def main():
    logger = Logger()
    cleanScreen()

    localDb = ProdDb(logSql=False, autocommit=True)
    dataCenterDb = CcxDataCenter(logSql=False, autocommit=True)
    fcdb = FCDB(logSql=False, autocommit=True)
    cnabsDb = CnabsDb(logSql=False, autocommit=True)

    t = time.perf_counter()

    logger.info("========ABS DATA同步 开始 ========")
    absClass = SyncAbsDeals(logger=logger, localDb=localDb, absDb=cnabsDb)
    absTables = SyncAbsTables(logger=logger, localDb=localDb, absDb=cnabsDb)
    absBigData = SyncAbsBigData(logger=logger, localDb=localDb, absDb=cnabsDb)
    # 产品大全
    logger.info("========产品大全 开始 ========")
    absClass.__updAbsDealsInfo__()
    #
    # 入池资产特征
    logger.info("========入池资产特征 开始 ========")
    absClass.__updAbsDealsProperty__()

    # 入池资产分布
    logger.info("========入池资产分布 开始 ========")
    absClass.__updAbsDealsDisb__()
    #
    # 证券大全
    logger.info("========证券大全 开始 ========")
    absClass.__updAbsBonds__()

    # 资产存续期-不良类
    logger.info("========资产存续期-不良类 开始 ========")
    absClass.__updNonPerforming__()

    # 资产存续期-债权类
    logger.info("========资产存续期-债权类 开始 ========")
    absClass.__updCreditorRights__()

    # # 归集现金流 废弃
    # logger.info("========归集现金流 开始 ========")
    # absClass.__updCashFlows__()
    #
    # abs_deals_tag
    logger.info("========abs_deals_tag类 开始 ========")
    absTables.__updAbsTagInfo__()

    # org_rating_config
    logger.info("=======org_rating_config类 开始 ========")
    absTables.__updRatingConfigInfo__()

    # # abs_bonds_rating
    logger.info("=======abs_bonds_rating类 开始 ========")
    absTables.__updAbsBondsRating__()

    # # abs_assets_distribution
    logger.info("=======abs_assets_distribution类 开始 ========")
    absTables.__updAbsAssetsDistribution__()

    # # abs_assets_collection_cashflows
    logger.info("=======abs_assets_collection_cashflows类 开始 ========")
    absTables.__updAbsAssetsCollectionCashflows__()
    # #
    # 证券存续期
    logger.info("======== 证券存续期 开始 ========")
    absClass.__updCurrentCash__()

    # 二级成交
    logger.info("======== 二级成交 开始 ========")
    absClass.__updSecondary__()

    # 证券存量统计
    logger.info("======== 证券存量统计 开始 ========")
    absBigData.__updStockData__()

    # 归集现金流
    logger.info("========abs_assets_promised_cashflows 开始 ========")
    absTables.__updPromisedCashFlows__()

    # 增信措施
    logger.info("======== 增信措施 开始 ========")
    absClass.__updCreditEnhancement__()

    # abs_bonds_trade
    logger.info("=======abs_bonds_trade类 开始 ========")
    absTables.__updAbsBondsTrade__()

    # abs_cross_sectional_datas
    logger.info("=======abs_cross_sectional_datas类 开始 ========")
    absTables.__updAbsCrossSectionalDatas__()

    # abs_bonds_cashflows
    logger.info("=======abs_bonds_cashflows类 开始 ========")
    absTables.__updAbsBondsCashFlows__()

    cast = (time.perf_counter() - t)
    m = math.floor(cast / 60)
    s = cast - m * 60
    logger.info('总耗时: %d分%.3f秒' % (m, s))
    logger.info("========ABS DATA同步 结束 ========")


# 程序入口
if __name__ == "__main__":
    main()
